﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sydfdl.TMS.BLL;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.Areas.Customer.Controllers
{
    public class GiftController : Controller
    {
        //
        // GET: /Customer/Gift/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Edit()
        {
            return View();
        }

        public void DelImage()
        {
            Guid GID = Guid.Parse(Request.Form.Get("GID"));
            string imgName = Request.Form.Get("ImgName") ?? "txt";
            string _url = Server.MapPath(string.Format("{0}{1}.{2}", @"~/Images/", GID.ToString(), imgName));
            Response.Write(StringHandling.DelImage(_url));
        }

        public void GetImage()
        {
            Guid GID = Guid.Parse(Request.Form.Get("GID"));
            T_C_Gift dto = new BLLT_C_Gift().GetImage(GID);
            string _xml = "";
            if (dto.ImgName != null)
            {
                _xml = Server.MapPath(string.Format("{0}{1}.{2}", @"~/Images/", GID.ToString(), dto.ImgName));
                StringHandling.saveImageByte(dto.ProdImg, _xml);
                _xml = string.Format(@"../../../../Images/{0}.{1}", GID.ToString(), dto.ImgName);
            }
            Response.Write(_xml);
        }

        public void SaveImage()
        {
            HttpPostedFileBase uploadFile = Request.Files.Get("gimage");
            Guid GID = Guid.Parse(Request.Form.Get("GID"));
            // 如果有文件, 则保存到一个地址
            string fileName = uploadFile.FileName.Split('.').Last();
            string _xml = string.Format(@"../../../../Images/{0}.{1}", GID.ToString(), fileName);
            if (uploadFile.ContentLength > 0)
            {
                string _url = Server.MapPath(string.Format("{0}{1}.{2}", @"~/Images/", GID.ToString(), fileName));
                uploadFile.SaveAs(_url);
                byte[] _i = StringHandling.getImageByte(_url);
                new BLLT_C_Gift().UpDataImage(_i, fileName, GID);
            }
            Response.Write(_xml);
        }

        public void GetDict()
        {
            string xml = JsonHelp.GetParaXML(Request.Form.Get("pageIndex"), Request.Form.Get("pageSize"),
                Request.Form.Get("sortField"), Request.Form.Get("sortOrder"), Request.Form.Get("key"));

            Response.Write(JsonHelp.EntitiesToString(new BLLT_C_Gift().GetList(xml)));
        }

        public void Save()
        {
            string json = @"[]";
            IEnumerable<T_C_Gift> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_C_Gift>>(Request.Form.Get("add"));
            IEnumerable<T_C_Gift> edit = Request.Form.Get("edit") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_C_Gift>>(Request.Form.Get("edit"));
            IEnumerable<T_C_Gift> del = Request.Form.Get("del") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_C_Gift>>(Request.Form.Get("del"));

            Response.Write(new BLLT_C_Gift().Save(add, edit, del));
        }

    }
}
